//
//  ViewReuserPool.m
//  tableView重用机制索引条
//
//  Created by 崔博 on 2018/6/20.
//  Copyright © 2018年 崔博. All rights reserved.
//

#import "ViewReuserPool.h"

@interface ViewReuserPool()
// 等待使用的队列
@property (nonatomic, strong) NSMutableSet *waiteUsedQueue;
// 使用中的队列
@property (nonatomic, strong) NSMutableSet *usingQueue;
@end
@implementation ViewReuserPool
- (instancetype)init
{
    self = [super init];
    if (self) {
        _waiteUsedQueue = [NSMutableSet set];
        _usingQueue = [NSMutableSet set];
    }
    return self;
}

- (UIView *)dequeueReuserableView
{
    UIView *view = [_waiteUsedQueue anyObject];
    if (view == nil) {
        return nil;
    }else
    {
        // 进行队列移动
        [_waiteUsedQueue removeObject:view];
        [_usingQueue removeObject:view];
        return view;
    }
}
- (void)addUsingView:(UIView *)view
{
    if (view == nil) {
        return;
    }
    [_usingQueue addObject:view];
}
- (void)reset
{
    UIView * view = nil;
    while ((view = [_usingQueue anyObject])) {
        // 从使用队列移除
        [_usingQueue removeObject:view];
        // 加入等待队列
        [_waiteUsedQueue addObject:view];
    }
}
@end
